package com.producer.广播队列;


import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class FanoutQueueConfig {
    /**
     * 扇形交换机名称
     */
    public static final String FANOUT_EXCHANGE_NAME = "fanoutExchange";

    /**
     * 扇形交换机
     */
    @Bean("fanoutExchange")
    public FanoutExchange createDeadExchange(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin){
        FanoutExchange fanoutExchange = new FanoutExchange(FANOUT_EXCHANGE_NAME,false,false);
        rabbitAdmin.declareExchange(fanoutExchange);
        return fanoutExchange;
    }

    public static final String FANOUT_QUEUE_ROUTINGKEY = "fanoutQueueRoutingkey";

    public static final String FIRST_FANOUT_QUEUE_NAME = "firstFanoutQueue";
    /**
     * 生成一个队列，绑定扇形交换机
     */
    @Bean("firstFanoutQueue")
    public Queue createFirstFanoutQueue(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin, @Qualifier("fanoutExchange") FanoutExchange exchange){
        Queue firstFanoutQueue = new Queue(FIRST_FANOUT_QUEUE_NAME,false,false,false);
        rabbitAdmin.declareQueue(firstFanoutQueue);
        rabbitAdmin.declareBinding(BindingBuilder.bind(firstFanoutQueue).to(exchange));
        return firstFanoutQueue;
    }

    public static final String SECOND_FANOUT_QUEUE_NAME = "secondFanoutQueue";

    @Bean("secondFanoutQueue")
    public Queue createSecondFanoutQueue(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin,
                                    @Qualifier("fanoutExchange") FanoutExchange exchange){
        Queue secondFanoutQueue = new Queue(SECOND_FANOUT_QUEUE_NAME,false,false,false);
        rabbitAdmin.declareQueue(secondFanoutQueue);
        rabbitAdmin.declareBinding(BindingBuilder.bind(secondFanoutQueue).to(exchange));
        return secondFanoutQueue;
    }
}
